package cn.hrc.controller;

import cn.hrc.dao.ClzDao;
import cn.hrc.dao.CouDao;
import cn.hrc.dao.TaskDao;
import cn.hrc.dao.UserDao;
import cn.hrc.entity.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/adm/task")
public class AdmTaskController {
    @Autowired
    TaskDao taskDao;
    @Autowired
    ClzDao clzDao;
    @Autowired
    CouDao couDao;
    @Autowired
    UserDao userDao;
    @RequestMapping("/show")
    public String taskShow(Model model, HttpSession session){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        List<TaskInfo> tasks = taskDao.findAll();
        List<UserInfo> users = userDao.findByRole("tea");
        List<Course> courses = couDao.findAll();
        List<Clz> clzs = clzDao.findAll();

        model.addAttribute("users", users);
        model.addAttribute("courses",courses);
        model.addAttribute("clzs",clzs);
        model.addAttribute("tasks",tasks);
        return "adm/taskShow";
    }
    @PostMapping("/add")
    public String addTask(Task task,HttpSession session,Model model){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        try {
            System.out.println(task);
            int sql = taskDao.addTask(task);
            System.out.println("add【sql】:"+sql);
        } catch (Exception e) {
            System.out.println("添加失败");
        }
        return "forward:/adm/task/show";
    }
    @GetMapping("/delete/{kid}")
    public String deleteTask(@PathVariable("kid")Integer kid,HttpSession session,Model model){
        User who = (User)session.getAttribute("who");
        if(who == null){
            model.addAttribute("loginmsg","请先登录在调用该功能");
            //redirect:
            return "/login";
        }
        System.out.println("要删除的排课ID："+kid);
        int sql = taskDao.deleteTask(kid);
        return "forward:/adm/task/show";
    }
}
